package com.ctgu.demo05.controller;

import com.ctgu.demo05.service.CustomersService;
import com.ctgu.demo05.utils.ApiResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * @ClassName OrderController
 * @Author Alex2
 * @Date 2025/3/13 10:54
 **/
@RestController
@RequestMapping("/api/orders")
public class OrderController {

    @Autowired
    private CustomersService customersService;

    /**
     * @Author: Alex
     * @Description: 插入顾客
     */
    // http://localhost:8080/api/orders/insertCustomer?name=Alex&balance=1000
    @GetMapping("insertCustomer")
    public ApiResult insertCustomer(@RequestParam("name") String name, @RequestParam("balance") BigDecimal balance) {
        return customersService.insertCustomer(name, balance);
    }

    /**
     * @Author: Alex
     * @Description: 编程式事务
     */
    // http://localhost:8080/api/orders/consumer?customerId=1&amount=50
    @GetMapping("consumer")
    public ApiResult consumer(@RequestParam("customerId") Integer customerId, @RequestParam("amount") BigDecimal amount) {
        return customersService.consumer(customerId, amount);
    }

    /**
     * @Author: Alex
     * @Description: 注解式事务-乐观锁+原子 SQL
     */
    // http://localhost:8080/api/orders/updateBalanceAndCreateOrder?customerId=1&amount=50
    @GetMapping("updateBalanceAndCreateOrder")
    public ApiResult updateBalanceAndCreateOrder(@RequestParam("customerId") Integer customerId, @RequestParam("amount") BigDecimal amount) {
        return customersService.updateBalanceAndCreateOrder(customerId, amount);
    }
}